import sequelize from '../config/database.js';
import Transaction from './Transaction.js';
import Category from './Category.js';

// 定义模型关联关系
Transaction.belongsTo(Category, {
  foreignKey: 'categoryId',
  as: 'category'
});

Category.hasMany(Transaction, {
  foreignKey: 'categoryId',
  as: 'transactions'
});

// 同步数据库表结构
const syncDatabase = async () => {
  try {
    await sequelize.sync({ force: false }); // force: false 保留现有数据
    console.log('✅ 数据库表同步成功');
  } catch (error) {
    console.error('❌ 数据库表同步失败:', error);
    throw error;
  }
};

export {
  Transaction,
  Category,
  syncDatabase,
  sequelize
};

export default {
  Transaction,
  Category,
  syncDatabase,
  sequelize
};